//
//  RCTConvert+MapSearch.m
//  YuanXinBaiduMap
//
//  Created by 晏德智 on 16/10/9.
//  Copyright © 2016年 晏德智. All rights reserved.
//

#import "RCTConvert+MapSearch.h"

#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>

@implementation RCTConvert(MapSearch)

+ (BMKGeoCodeSearchOption *)BMKGeoCodeSearchOption:(id)json
{
    json = [self NSDictionary:json];
    BMKGeoCodeSearchOption *geoCodeSearchOption = [BMKGeoCodeSearchOption new];
    geoCodeSearchOption.city = [self NSString:json[@"city"]];
    geoCodeSearchOption.address = [self NSString:json[@"address"]];

    return geoCodeSearchOption;
}

RCT_ARRAY_CONVERTER(BMKGeoCodeSearchOption)

+ (BMKReverseGeoCodeSearchOption *)BMKReverseGeoCodeSearchOption:(id)json
{
    json = [self NSDictionary:json];
    BMKReverseGeoCodeSearchOption *reverseGeoCodeOption = [BMKReverseGeoCodeSearchOption new];
    reverseGeoCodeOption.location = [self CLLocationCoordinate2D:json];
    return reverseGeoCodeOption;
}

RCT_ARRAY_CONVERTER(BMKReverseGeoCodeSearchOption)

+ (BMKPOICitySearchOption *)BMKPOICitySearchOption:(id)json
{
    json = [self NSDictionary:json];
    BMKPOICitySearchOption *citySearchOption = [BMKPOICitySearchOption new];
    //[self NSInteger:json[@"viewIndex"] ? :@(NSNotFound)];
    citySearchOption.pageIndex =  [self NSInteger:json[@"pageIndex"] ? :0];
    citySearchOption.pageSize = [self NSInteger:json[@"pageCapacity"] ? : @(10)];
    citySearchOption.city=  [self NSString:json[@"city"]];
    citySearchOption.keyword =  [self NSString:json[@"keyword"]];
    return citySearchOption;
}

RCT_ARRAY_CONVERTER(BMKPOICitySearchOption)

+ (BMKPOINearbySearchOption *)BMKPOINearbySearchOption:(id)json
{
    json = [self NSDictionary:json];
    BMKPOINearbySearchOption *citySearchOption = [BMKPOINearbySearchOption new];
    citySearchOption.pageIndex =  [self NSInteger:json[@"pageIndex"] ? : 0];
    citySearchOption.pageSize = [self NSInteger:json[@"pageCapacity"] ? : @(10)];
    NSMutableArray *mArr = NSMutableArray.new;
    if ([self NSString:json[@"keywords"]]) {
        [mArr addObjectsFromArray:[[self NSString:json[@"keywords"]] componentsSeparatedByString:@","]];
    }
    if ([self NSString:json[@"keywords"]]) {
        [mArr addObject:[self NSString:json[@"keyword"]]];
    }
    citySearchOption.keywords = mArr;
    citySearchOption.radius = [self NSInteger:json[@"radius"]];
    citySearchOption.location= [self CLLocationCoordinate2D:json[@"location"]];
    
    return citySearchOption;
}
RCT_ARRAY_CONVERTER(BMKPOINearbySearchOption)
+ (BMKPOIBoundSearchOption *)BMKPOIBoundSearchOption:(id)json
{
    json = [self NSDictionary:json];
    /*
     ///矩形区域，左下角和右上角的经纬度坐标点。
     @property (nonatomic, assign) CLLocationCoordinate2D leftBottom;
     @property (nonatomic, assign) CLLocationCoordinate2D rightTop;
     */
    BMKPOIBoundSearchOption *boundSearchOption = [BMKPOIBoundSearchOption new];
    boundSearchOption.pageIndex =  [self NSInteger:json[@"pageIndex"] ? :0];
    boundSearchOption.pageSize = [self NSInteger:json[@"pageCapacity"] ? : @(10)];
    NSMutableArray *mArr = NSMutableArray.new;
    if ([self NSString:json[@"keywords"]]) {
        [mArr addObjectsFromArray:[[self NSString:json[@"keywords"]] componentsSeparatedByString:@","]];
    }
    if ([self NSString:json[@"keywords"]]) {
        [mArr addObject:[self NSString:json[@"keyword"]]];
    }
    boundSearchOption.keywords =  mArr;
    //citySearchOption.radius = [self NSInteger:json[@"radius"]];
    boundSearchOption.leftBottom= [self CLLocationCoordinate2D:json[@"leftBottom"]];
    boundSearchOption.rightTop= [self CLLocationCoordinate2D:json[@"rightTop"]];
    
    return boundSearchOption;
}
RCT_ARRAY_CONVERTER(BMKPOIBoundSearchOption)


@end
